package com.ylfstu.list_;

public class LinkedList01 {
    public static void main(String[] args) {
        //简单的双向列表
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node ylf = new Node("ylf");

        //连接3个结点，形成双向链表
        jack.next = tom;
        tom.next = ylf;
        ylf.pre = tom;
        tom.pre = jack;

        Node first = jack;
        Node last = ylf;

        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first);
            first = first.next;
        }
        while (true) {
            if (last == null) {
                break;
            }
            System.out.println(last);
            last = last.pre;
        }

        Node lily = new Node("Lily");
        lily.next = ylf;
        lily.pre = tom;
        tom.next = lily;
        ylf.pre = lily;

        first = jack;
        last = ylf;
        System.out.println("=====增加后=====");
        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first);
            first = first.next;
        }
        while (true) {
            if (last == null) {
                break;
            }
            System.out.println(last);
            last = last.pre;
        }

    }
}

//定义一个Node类
class Node {
    public Object item;//真正存放数据的地方
    public Node next;
    public Node pre;

    public Node(Object name) {
        this.item = name;
    }

    @Override
    public String toString() {
        return "Node Name=" + item;
    }
}
